/*
 * Copyright (c) 2022, CCSSOFT All Rights Reserved.
 */
package net.sf.log4jdbc.http;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**

 * @author zhanxb
 * @since 2022/6/20
 **/
public class HttpUtils
{

    private HttpUtils()
    {
        throw new IllegalStateException("Utility class, cannot be instantiated!");
    }

    public static Map<String, String> formData2Map(String formData)
    {
        Map<String, String> result = new HashMap<String, String>();
        if (formData == null || formData.trim().length() == 0)
        {
            return result;
        }
        final String[] items = formData.split("&");
        for (String s : items) {
            final String[] keyAndVal = s.split("=");
            if (keyAndVal.length == 2)
            {
                try
                {
                    final String key = URLDecoder.decode(keyAndVal[0], "utf8");
                    final String val = URLDecoder.decode(keyAndVal[1], "utf8");
                    result.put(key, val);
                } catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
